<HTML><HEAD><TITLE>mod(+Number1, +Number2, -Result)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Arithmetic</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>mod(+Number1, +Number2, -Result)</H1>
Evaluates the modulus Number1 mod Number2 and unifies the resulting value
with Result.


<DL>
<DT><EM>Number1</EM></DT>
<DD>Integer.
</DD>
<DT><EM>Number2</EM></DT>
<DD>Integer.
</DD>
<DT><EM>Result</EM></DT>
<DD>A variable or integer.
</DD>
</DL>
<H2>Description</H2>
   This predicate is used by the ECLiPSe compiler to expand evaluable
   arithmetic expressions.  So the call to mod(Number1, Number2, Result) is
   equivalent to
<PRE>
    Result is Number1 mod Number2
</PRE>
    which should be preferred for portability.
<P>
    The modulus operation computes the remainder corresponding to the
    flooring division div.  The following relation always holds:
<PRE>
    X =:= (X mod Y) + (X div Y) * Y.
</PRE>
    The result Result is either zero, or has the same sign as Number2.
<P>
   In coroutining mode, if Number1 or Number2 are uninstantiated, the call
   to mod/3 is delayed until these variables are instantiated.
<P>
   CAUTION: The behaviour of mod was changed for standard compliance!
   In ECLiPSe versions up to 5.8, mod computed the remainder corresponding
   to the truncating division //, and thus gave different results for
   arguments with opposite signs.  Moreover, the operator precedence was
   changed from op(300,xfx,mod) to op(400,yfx,mod), which means that
   a*b mod c is now parsed as (a*b)mod c rather than a*(b mod c).

<H3>Modes and Determinism</H3><UL>
<LI>mod(+, +, -) is det
</UL>
<H3>Exceptions</H3>
<DL>
<DT><EM>(4) instantiation fault </EM>
<DD>Number1 or Number2 is not instantiated (non-coroutining mode    only).
<DT><EM>(5) type error </EM>
<DD>Number1 or Number2 is a number but not an integer.
<DT><EM>(20) arithmetic exception </EM>
<DD>Illegal arithmetic operation: Number2 is zero
<DT><EM>(24) number expected </EM>
<DD>Number1 or Number2 is not of a numeric type.
</DL>
<H2>Examples</H2>
<PRE>
Success:
      X is 10 mod 3.		(gives X = 1)

      mod( 10,  3,  1).
      mod(-10,  3,  2).
      mod( 10, -3, -2).
      mod(-10, -3, -1).

      mod( 11,  3,  2).
Fail:
      mod(1, 2, 3).
      mod(6, 2.0, 3.0).
      mod(5, 2, r).
Error:
      mod(A, 2, 6).              (Error 4).
      mod(2, 0, Result).         (Error 20).
      mod(4 + 2, 2, 12).         (Error 24).
</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/arithmetic/is-2.html">is / 2</A>, <A HREF="../../kernel/arithmetic/div-3.html">div / 3</A>, <A HREF="../../kernel/arithmetic/rem-3.html">rem / 3</A>
</BODY></HTML>
